import { AssetManager } from "cc";
import { BundleMgr } from "../../mgr/BundleMgr";
import { ResMgr } from "../../mgr/ResMgr";
import { ViewId } from "../view/IView";
import { ILoading } from "./ILoading";


export class LoadingBundle implements ILoading {

    constructor(
        public resMgr: ResMgr,
        public bundleMgr: BundleMgr
    ) {

    }

    bLoading: boolean;
    bCompleted: boolean;
    loadedProgress: number = 0;
    readonly totalProgress: number = 3;
    load(): void {
        if (this.bLoading) {
            return;
        }
        this.bLoading = true;

        this.resMgr.loadBundle(ViewId.NONE, "public").then((bundle: AssetManager.Bundle) => {
            this.bundleMgr.public = bundle;
            this.loadedProgress++;
        })

        this.resMgr.loadBundle(ViewId.NONE, "jszip").then((bundle: AssetManager.Bundle) => {
            this.bundleMgr.jszip = bundle;
            this.loadedProgress++;
        })

        this.resMgr.loadBundle(ViewId.NONE, "config").then((bundle: AssetManager.Bundle) => {
            this.bundleMgr.config = bundle;
            this.loadedProgress++;
        })
    }
    checkCompleted(): boolean {
        if (this.bCompleted) {
            return true;
        }
        const ret = this.loadedProgress >= this.totalProgress;        
        if (ret) {
            this.bCompleted = ret;
            this.target.onLoadingCompleted();
            return true;
        }
        return false;
    }
    getProgress(): number {
        return this.loadedProgress / this.totalProgress;
    }
    getProgressDesc(): string {
        return "正在加载bundle" + this.getProgress();
    }
    target: { onLoadingCompleted(): void };
}